enum ContentMediaType { imageCollection, videoClip }

enum SubmissionCategory { challengeShare, dailyShare }

/// 媒体资源实体
class MediaAsset {
  final String assetId;
  final String localPath;
  final ContentMediaType mediaType;
  final String? thumbnailPath; // 视频缩略图路径
  final DateTime uploadedAt;

  const MediaAsset({
    required this.assetId,
    required this.localPath,
    required this.mediaType,
    this.thumbnailPath,
    required this.uploadedAt,
  });

  factory MediaAsset.fromJson(Map<String, dynamic> json) {
    return MediaAsset(
      assetId: json['assetId'],
      localPath: json['localPath'],
      mediaType: ContentMediaType.values[json['mediaType']],
      thumbnailPath: json['thumbnailPath'],
      uploadedAt: DateTime.parse(json['uploadedAt']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'assetId': assetId,
      'localPath': localPath,
      'mediaType': mediaType.index,
      'thumbnailPath': thumbnailPath,
      'uploadedAt': uploadedAt.toIso8601String(),
    };
  }
}

/// 挑战任务实体
class ChallengeTask {
  final String taskId;
  final String taskTitle;
  final String taskTag;
  final String taskDescription;

  const ChallengeTask({
    required this.taskId,
    required this.taskTitle,
    required this.taskTag,
    required this.taskDescription,
  });

  factory ChallengeTask.fromJson(Map<String, dynamic> json) {
    return ChallengeTask(
      taskId: json['taskId'],
      taskTitle: json['taskTitle'],
      taskTag: json['taskTag'],
      taskDescription: json['taskDescription'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'taskId': taskId,
      'taskTitle': taskTitle,
      'taskTag': taskTag,
      'taskDescription': taskDescription,
    };
  }
}

/// 用户互动实体
class UserInteraction {
  final String interactionId;
  final String userId;
  final String userName;
  final String userAvatar;
  final DateTime interactedAt;

  const UserInteraction({
    required this.interactionId,
    required this.userId,
    required this.userName,
    required this.userAvatar,
    required this.interactedAt,
  });

  factory UserInteraction.fromJson(Map<String, dynamic> json) {
    return UserInteraction(
      interactionId: json['interactionId'],
      userId: json['userId'],
      userName: json['userName'],
      userAvatar: json['userAvatar'],
      interactedAt: DateTime.parse(json['interactedAt']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'interactionId': interactionId,
      'userId': userId,
      'userName': userName,
      'userAvatar': userAvatar,
      'interactedAt': interactedAt.toIso8601String(),
    };
  }
}

/// 内容分享实体
class ContentSubmission {
  final String submissionId;
  final String authorId;
  final String authorName;
  final String authorAvatar;
  final SubmissionCategory category;
  final String textDescription;
  final List<MediaAsset> mediaAssets;
  final ChallengeTask? associatedTask; // 仅挑战分享时有值
  final List<UserInteraction> appreciations; // 点赞列表
  final int appreciationCount;
  final DateTime publishedAt;
  final DateTime lastModified;

  const ContentSubmission({
    required this.submissionId,
    required this.authorId,
    required this.authorName,
    required this.authorAvatar,
    required this.category,
    required this.textDescription,
    required this.mediaAssets,
    this.associatedTask,
    required this.appreciations,
    required this.appreciationCount,
    required this.publishedAt,
    required this.lastModified,
  });

  factory ContentSubmission.fromJson(Map<String, dynamic> json) {
    return ContentSubmission(
      submissionId: json['submissionId'],
      authorId: json['authorId'],
      authorName: json['authorName'],
      authorAvatar: json['authorAvatar'],
      category: SubmissionCategory.values[json['category']],
      textDescription: json['textDescription'],
      mediaAssets: (json['mediaAssets'] as List)
          .map((asset) => MediaAsset.fromJson(asset))
          .toList(),
      associatedTask: json['associatedTask'] != null
          ? ChallengeTask.fromJson(json['associatedTask'])
          : null,
      appreciations: (json['appreciations'] as List)
          .map((interaction) => UserInteraction.fromJson(interaction))
          .toList(),
      appreciationCount: json['appreciationCount'],
      publishedAt: DateTime.parse(json['publishedAt']),
      lastModified: DateTime.parse(json['lastModified']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'submissionId': submissionId,
      'authorId': authorId,
      'authorName': authorName,
      'authorAvatar': authorAvatar,
      'category': category.index,
      'textDescription': textDescription,
      'mediaAssets': mediaAssets.map((asset) => asset.toJson()).toList(),
      'associatedTask': associatedTask?.toJson(),
      'appreciations': appreciations.map((interaction) => interaction.toJson()).toList(),
      'appreciationCount': appreciationCount,
      'publishedAt': publishedAt.toIso8601String(),
      'lastModified': lastModified.toIso8601String(),
    };
  }

  /// 复制并更新点赞信息
  ContentSubmission copyWithAppreciation({
    List<UserInteraction>? appreciations,
    int? appreciationCount,
  }) {
    return ContentSubmission(
      submissionId: submissionId,
      authorId: authorId,
      authorName: authorName,
      authorAvatar: authorAvatar,
      category: category,
      textDescription: textDescription,
      mediaAssets: mediaAssets,
      associatedTask: associatedTask,
      appreciations: appreciations ?? this.appreciations,
      appreciationCount: appreciationCount ?? this.appreciationCount,
      publishedAt: publishedAt,
      lastModified: DateTime.now(),
    );
  }

  /// 检查用户是否已点赞
  bool isAppreciatedByUser(String userId) {
    return appreciations.any((interaction) => interaction.userId == userId);
  }
}
